<template>
    <!-- pages/orderDetail/orderDetail.wxml -->

    <view class="container">
        <view class="top-container">
            <view class="top">
                <view class="info">
                    <view>
                        <image class="roomImg" :src="magic.split(OrderInfodata.roomImg)[0]"></image>
                    </view>
                    <view class="name">
                        {{ OrderInfodata.roomName }}（{{
                            OrderInfodata.roomType == 1
                                ? '小包'
                                : OrderInfodata.roomType == 2
                                ? '中包'
                                : OrderInfodata.roomType == 3
                                ? '大包'
                                : OrderInfodata.roomType == 4
                                ? '豪包'
                                : '商务包'
                        }}）
                    </view>
                    <view class="share">
                        <button class="share bg-primary" open-type="share"><text>分享</text></button>
                    </view>
                </view>
            </view>
            <!-- 时间段 -->
            <view class="timeBox">
                <view class="time">
                    <view class="hour">{{ magic.splittime(OrderInfodata.startTime)[0] }}:{{ magic.splittime(OrderInfodata.startTime)[1] }}</view>
                    <view class="date">{{ magic.splitkongge(OrderInfodata.startTime)[0] }}</view>
                </view>
                <view class="total border-primary">{{ OrderInfodata.orderHour }}小时</view>
                <view class="time">
                    <view class="hour">{{ magic.splittime(OrderInfodata.endTime)[0] }}:{{ magic.splittime(OrderInfodata.endTime)[1] }}</view>
                    <view class="date">{{ magic.splitkongge(OrderInfodata.endTime)[0] }}</view>
                </view>
            </view>
            <!-- 订单详情 -->
            <view class="orderInfo">
                <view class="item">
                    <view class="title">门店名称：</view>
                    <view class="logo">
                        {{ OrderInfodata.storeName }}
                        <text class="color-primary link" @tap="goTencentMap">查看地图</text>
                    </view>
                </view>
                <view class="item">
                    <view class="title">支付方式：</view>
                    <text>{{ OrderInfodata.payType === 1 ? '微信' : OrderInfodata.payType === 2 ? '余额' : '团购' }}</text>
                </view>
                <view class="item">
                    <label class="title">使用卡券：</label>
                    <block v-if="OrderInfodata.couponName.length > 0">
                        <text>{{ OrderInfodata.couponName }}</text>
                    </block>
                    <block v-else>
                        <text>暂无</text>
                    </block>
                </view>
                <view class="item">
                    <label class="title">下单时间：</label>
                    <text>{{ OrderInfodata.createTime }}</text>
                </view>
                <!-- wifi -->
                <view class="item" v-if="OrderInfodata.status == 0 || OrderInfodata.status == 1">
                    <label class="title">WIFI信息：</label>
                    <view class="color-primary wifi">
                        {{ OrderInfodata.wifiInfo }}
                    </view>
                </view>
            </view>
        </view>
        <view class="order-container">
            <view class="item" style="align-items: center">
                <label>请选择续费时长：</label>
                <van-stepper :value="addTime" step="1" min="0" max="8" disable-input @change="onChange" />
                时
            </view>
            <view class="item">
                <label>增加后结束时间：</label>
                <text>{{ newTime }}</text>
            </view>
            <view class="item">
                <label>小时单价：</label>
                <text class="color-attention">{{ OrderInfodata.roomPrice }}元</text>
                /小时
            </view>
            <!-- <block wx:if="{{OrderInfodata.roomPrice!=OrderInfodata.workPrice}}">
      <view class="item">
        <label>闲时单价：</label>
        <text class="color-attention">{{OrderInfodata.workPrice}}元</text>
        /小时
      </view>
    </block> -->
            <view class="item">
                <label>支付总价：</label>
                <text class="color-attention">{{ totalPay }}</text>
                元
            </view>

            <view class="btns">
                <button class="backBtn" @tap="backHome">返回首页</button>
                <block v-if="isLogin">
                    <button class="renewBtn" @tap="renewOrder">订单续费</button>
                </block>
                <block v-else>
                    <button class="renewBtn" @getphonenumber="phone" open-type="getPhoneNumber">订单续费</button>
                </block>
            </view>
        </view>
    </view>
</template>
<script module="magic" lang="wxs" src="@/utils/magic.wxs"></script>
<script>
// pages/roomRenew/roomRenew.js
const app = getApp();
var http = require('../../utils/http');
var Moment = require('../../lib/moment.js');
export default {
    data() {
        return {
            isLogin: app.globalData.isLogin,
            storeId: '',
            roomId: '',

            OrderInfodata: {
                roomName: '',
                roomType: 0,
                orderHour: '',
                payType: 0,
                couponName: [],
                createTime: '',
                status: 0,
                wifiInfo: '',
                roomPrice: ''
            },

            newTime: '',
            totalPay: '',
            addTime: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        var that = this;
        var storeId = '';
        var roomId = '';
        if (options.storeId) {
            storeId = options.storeId;
        }
        if (options.roomId) {
            roomId = options.roomId;
        }
        var query = uni.getEnterOptionsSync().query;
        if (query) {
            if (query.storeId) {
                storeId = query.storeId;
            }
            if (query.roomId) {
                roomId = query.roomId;
            }
        }
        that.setData({
            storeId: storeId,
            roomId: roomId
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this;
        setTimeout(() => {
            that.setData({
                isLogin: app.globalData.isLogin
            });
        }, 200);
        that.getOrderInfo();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        getOrderInfo: function () {
            var that = this;
            http.request(
                '/member/order/getOrderByRoomId/' + that.roomId,
                '1',
                'post',
                {},
                app.globalData.userDatatoken.accessToken,
                '获取中...',
                function success(info) {
                    console.info('订单信息===');
                    // console.info(info);
                    if (info.code === 0) {
                        that.setData({
                            OrderInfodata: info.data
                        });
                    } else {
                        uni.showModal({
                            title: '温馨提示',
                            content: info.msg,
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    let pages = getCurrentPages();
                                    if (pages.length > 1) {
                                        uni.navigateBack({
                                            //返回
                                            delta: 1
                                        });
                                    }
                                    if (pages.length == 1) {
                                        uni.reLaunch({
                                            url: '/pages/index/index'
                                        });
                                    }
                                }
                            }
                        });
                    }
                },
                function fail(info) {}
            );
        },

        // 续费加时间
        onChange: function (event) {
            var that = this;
            var addTime = event.detail;
            var newTime = Moment(that.OrderInfodata.endTime).add(addTime, 'hours').format('YYYY/MM/DD HH:mm');
            //console.log(`newtime:${newTime}`)
            this.setData({
                newTime: newTime,
                totalPay: (addTime * that.getPrice(newTime)).toFixed(2)
            });
        },

        getPrice: function (startDate) {
            var that = this;
            var day = new Date(startDate).getDay();
            switch (day) {
                case 1:
                case 2:
                case 3:
                case 4:
                    return that.OrderInfodata.workPrice;
                case 0:
                case 5:
                case 6:
                    return that.OrderInfodata.roomPrice;
            }
        },

        backHome: function () {
            uni.reLaunch({
                url: '/pages/index/index'
            });
        },

        renewOrder: function () {
            var that = this;
            if (!that.newTime) {
                uni.showModal({
                    title: '温馨提示',
                    content: '请选择需要增加的时间！',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {}
                });
                return;
            }
            if (app.globalData.isLogin) {
                http.request(
                    '/member/order/preOrder',
                    '1',
                    'post',
                    {
                        roomId: that.OrderInfodata.roomId,
                        couponId: '',
                        startTime: that.OrderInfodata.endTime,
                        endTime: that.newTime,
                        orderId: that.OrderInfodata.orderId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '提交中...',
                    function success(info) {
                        console.info('支付信息===');
                        console.info(info);
                        if (info.code == 0) {
                            that.renewOrderNo = info.data.orderNo;
                            that.lockWxOrder(info);
                        } else {
                            uni.showModal({
                                title: '温馨提示',
                                content: info.msg,
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {}
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 锁定微信订单
        lockWxOrder: function (pay) {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/order/lockWxOrder',
                    '1',
                    'post',
                    {
                        roomId: that.OrderInfodata.roomId,
                        couponId: '',
                        startTime: that.OrderInfodata.endTime,
                        endTime: that.newTime,
                        orderId: that.OrderInfodata.orderId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '提交中...',
                    function success(info) {
                        if (info.code == 0) {
                            console.log('锁定微信支付订单');
                            that.payMent(pay); //微信支付
                        } else {
                            uni.showModal({
                                title: '温馨提示',
                                content: info.msg,
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {}
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //支付
        payMent: function (pay) {
            var that = this;
            uni.requestPayment({
                timeStamp: pay.data.timeStamp,
                nonceStr: pay.data.nonceStr,
                package: pay.data.pkg,
                signType: pay.data.signType,
                paySign: pay.data.paySign,
                success: function (res) {
                    //console.log('*************支付成功');
                    // setTimeout(function () {
                    // }, 1000);
                    // that.getOrderInfo()
                },
                fail: function (res) {
                    uni.showToast({
                        title: '支付失败!',
                        icon: 'error'
                    });
                    //console.log('*************支付失败');
                },

                complete: function (res) {
                    //console.log('*************支付完成');
                }
            });
        },

        phone: function (e) {
            var that = this;
            //console.log('手机号码授权+++++++');
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
                uni.showToast({
                    title: '已取消授权'
                });
            }
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                //console.log('手机号码授权+++++++');
                uni.login({
                    success: function (res) {
                        if (res.code != null) {
                            http.request(
                                '/member/auth/weixin-mini-app-login',
                                '1',
                                'post',
                                {
                                    phoneCode: e.detail.code,
                                    loginCode: res.code
                                },
                                '',
                                '获取中...',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        if (info.data) {
                                            app.globalData.userDatatoken = info.data;
                                            app.globalData.isLogin = true;
                                            that.setData({
                                                isLogin: true
                                            });
                                            //缓存服务器返回的用户信息
                                            uni.setStorageSync('userDatatoken', info.data);
                                        }
                                    }
                                },
                                function fail(info) {}
                            );
                        } else {
                            //console.log('登录失败！' + res.errMsg)
                        }
                    }
                });
            }
        },

        goTencentMap() {
            console.log('占位：函数 goTencentMap 未声明');
        }
    }
};
</script>
<style>
@import './roomRenew.css';
</style>
